package org.abettor.util;

import java.io.File;

/**
 * 应用程序运行环境工具类
 * @author shawn
 *
 */
public abstract class Environment
{
	/**
	 * 获取当前应用程序的绝对安装路径
	 */
	public static String getApplicationHome()
	{
		File f = new File(".");
		String appPath = f.getAbsolutePath();
		if(appPath.endsWith(File.separator + "."))
		{
			appPath = appPath.substring(0, appPath.length() - 2);
		}
		if(appPath.endsWith(File.separator + "bin"))
		{
			appPath = appPath.substring(0, appPath.length() - 4);
		}

		return appPath;
	}

	/**
	 * 获取指定相对路径所指定的绝对路径
	 * @param relativePath
	 */
	public static String getFilePath(String relativePath)
	{
		String base = getApplicationHome();
		base = base == null ? File.separator : base;
		if(! base.endsWith(File.separator) && ! base.endsWith("/"))
		{
			base += File.separator;
		}

		String tmp = relativePath == null ? "" : relativePath;
		while(tmp.startsWith(File.separator) || tmp.startsWith("/"))
		{
			tmp = tmp.substring(1);
		}

		return base + tmp;
	}

	/**
	 * 获取用户数据目录
	 */
	public static String getUserDataDirectory()
	{
		String userDir = System.getProperty("user.home");
		if(! userDir.endsWith(File.separator))
		{
			userDir += File.separator;
		}

		String userDataDir = null;
		String osName = System.getProperty("os.name");
		if(osName.contains("Windows"))
		{
			userDataDir = "Local Settings\\Application Data\\angleworm";
		}
		else
		{
			userDataDir = ".angleworm";
		}

		return userDir + userDataDir;
	}
}
